package reflect;

import java.util.Scanner;

/**
 * @Classname: ReflectDemo2
 * @Author: bromide
 * @CreateTime: 2022/4/18--10:44
 * @Version: V1.0
 * @Description: 使用反射机制实例化对象
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //1获取要实例化的类的类对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要实例化的类名：");
        String className = scanner.nextLine();
        Class cls = Class.forName(className);
        //2类对象直接提供了可以通过公开的无参构造器实例化的功能
        Person person = (Person) cls.newInstance();
        System.out.println(person);
    }
}
